//
//  CTDatabase.m
//  Astro
//
//  Created by hero on 12/5/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CTDatabase.h"


@implementation CTDatabase

- (CTDatabase*) init{
	if (self = [super init]) {
		[CTDatabase copyDatabaseIfNeeded];
		sqlite3_open([[CTDatabase getDBPath] UTF8String], &database); 
	}
	
	return self;
}

+ (void) copyDatabaseIfNeeded {
	//Using NSFileManager we can perform many file system operations.
	NSFileManager *fileManager = [NSFileManager defaultManager];
	NSError *error;
	NSString *dbPath = [CTDatabase getDBPath];
	BOOL success = [fileManager fileExistsAtPath:dbPath]; 
	
	if(!success) {		
		NSLog(@"CopyDB");
		
		NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:DBName];
		success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
		
		if (!success) 
			NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
	}	
}

+ (NSString *) getDBPath {
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
	NSString *documentsDir = [paths objectAtIndex:0];
	return [documentsDir stringByAppendingPathComponent:DBName];
}

//----
- (NSString*) getToken:(NSInteger)cateId andSignId:(NSInteger) signId{
	NSString *token = nil;
	//	const char *sql = (tokenType == 0) ? "SELECT value	FROM config WHERE code = 0" : "SELECT value	FROM config WHERE code = 1";
//	NSString *temp = [NSString stringWithFormat:@"SELECT value	FROM content WHERE code = %d", tokenType];
//	NSString *temp = [NSString stringWithFormat:@"SELECT ContentInfoVN  FROM content WHERE SignID = %d and CateID = %d",signId, cateId];
	NSString *temp = [[NSString alloc] initWithFormat:@"SELECT ContentInfoVN  FROM content WHERE SignID = %d and CateID = %d",signId, cateId];
	
//	[temp retain];
	const char *sql = [temp UTF8String];
	NSLog(@"temp = %@", temp);
	NSLog(@"sql = %s", sql);
	
	sqlite3_stmt *selectstmt;
	
	if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
		while(sqlite3_step(selectstmt) == SQLITE_ROW) {			
			token = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];	
			sqlite3_finalize(selectstmt);
			break;
		}
	}
	
	NSLog(@"ResultToken: %@", token);
	
	return token;
	
		
}



//----
- (void) setToken:(NSString*)token WithTokenType:(NSInteger)tokenType{
	NSLog(@"Token to set: %@", token);
	
	char *update = "INSERT OR REPLACE INTO config (code, value) VALUES (?, ?);";
	sqlite3_stmt *stmt;
	
	if (sqlite3_prepare_v2(database, update, -1, &stmt, nil) == SQLITE_OK) {
		sqlite3_bind_int(stmt, 1, tokenType);
		sqlite3_bind_text(stmt, 2, [token UTF8String], -1, NULL);
	}
	
	if (sqlite3_step(stmt) != SQLITE_DONE)
		//		NSAssert1(0, @"Error updating table: %s", errorMsg);
		return;
	
	sqlite3_finalize(stmt);
}

//================== get icon of Category.
- (NSString*) getIconCategory:(NSInteger)cateId {
	NSString *iconName = nil;
	NSString *temp = [[NSString alloc] initWithFormat:@"SELECT Icon FROM category where CateID = %d", cateId];
	
	//	[temp retain];
	const char *sql = [temp UTF8String];
	
	sqlite3_stmt *selectstmt;
	
	if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
		while(sqlite3_step(selectstmt) == SQLITE_ROW) {			
			iconName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];	
			sqlite3_finalize(selectstmt);
			break;
		}
	}
	
//	NSLog(@"ResultToken: %@", iconName);
	
	return iconName;
	
	
}

//================== get icon of signs table.
- (NSString*) getIconSigns:(NSInteger)SignId {
	NSString *iconName = nil;
	NSString *temp = [[NSString alloc] initWithFormat:@"SELECT SignIcon FROM signs where SignID = %d", SignId];
	
	//	[temp retain];
	const char *sql = [temp UTF8String];
	
	sqlite3_stmt *selectstmt;
	
	if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
		while(sqlite3_step(selectstmt) == SQLITE_ROW) {			
			iconName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];	
			sqlite3_finalize(selectstmt);
			break;
		}
	}
	
//	NSLog(@"ResultToken: %@", iconName);
	
	return iconName;
	
	
}

@end
